package com.share.domain.rebate.service;

import com.share.domain.rebate.event.SendRebateMessageEvent;
import com.share.domain.rebate.model.aggregate.BehaviorRebateAggregate;
import com.share.domain.rebate.model.entity.BehaviorEntity;
import com.share.domain.rebate.model.entity.BehaviorRebateOrderEntity;
import com.share.domain.rebate.model.entity.TaskEntity;
import com.share.domain.rebate.model.valobj.DailyBehaviorRebateVo;
import com.share.domain.rebate.model.valobj.taskStateVo;
import com.share.domain.rebate.repository.IBehaviorRebateRepository;
import com.share.types.common.Constants;
import com.share.types.event.BaseEvent;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 行为返利服务实现
 */
@Service
public class BehaviorRebateService implements IBehaviorRebateService {
    @Resource
    private IBehaviorRebateRepository repository;

    @Resource
    private SendRebateMessageEvent sendRebateMessageEvent;

    @Override
    public List<String> createOrder(BehaviorEntity behaviorEntity) {
        //1.查询返利配置
        List<DailyBehaviorRebateVo> dailyBehaviorRebateVoList = repository.queryDailyBehaviorRebateConfig(behaviorEntity.getBehaviorTypeVo());
        if(null==dailyBehaviorRebateVoList||dailyBehaviorRebateVoList.isEmpty())return new ArrayList<>();

        //2.构建聚合对象
        List<String> orderIds = new ArrayList<>();
        List<BehaviorRebateAggregate> behaviorRebateAggregates = new ArrayList<>();

        for (DailyBehaviorRebateVo dailyBehaviorRebateVo : dailyBehaviorRebateVoList) {
            //bizId:用户id_返利类型_防重id
            String bizId = behaviorEntity.getUserId()+ Constants.UNDERLINE + dailyBehaviorRebateVo.getRebateType() + Constants.UNDERLINE+behaviorEntity.getOutBusinessNo();
            BehaviorRebateOrderEntity behaviorRebateOrderEntity = BehaviorRebateOrderEntity.builder()
                        .userId(behaviorEntity.getUserId())
                        .orderId(RandomStringUtils.randomNumeric(12))
                        .behaviorType(dailyBehaviorRebateVo.getBehaviorType())
                        .rebateDesc(dailyBehaviorRebateVo.getRebateDesc())
                        .rebateType(dailyBehaviorRebateVo.getRebateType())
                        .rebateConfig(dailyBehaviorRebateVo.getRebateConfig())
                        .outBusinessNo(behaviorEntity.getOutBusinessNo())
                        .bizId(bizId)
                        .build();
            orderIds.add(behaviorRebateOrderEntity.getOrderId());


            //mq消息
            SendRebateMessageEvent.RebateMessage rebateMessage = SendRebateMessageEvent.RebateMessage.builder()
                    .userId(behaviorEntity.getUserId())
                    .rebateType(dailyBehaviorRebateVo.getRebateType())
                    .rebateConfig(dailyBehaviorRebateVo.getRebateConfig())
                    .bizId(bizId)
                    .build();

            //构建时间消息
            BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage> rebateMessageEventMessage = sendRebateMessageEvent.buildBaseMessage(rebateMessage);
            //组装任务对象
            TaskEntity taskEntity = new TaskEntity();
            taskEntity.setUserId(behaviorEntity.getUserId());
            taskEntity.setMessageId(rebateMessageEventMessage.getId());
            taskEntity.setMessage(rebateMessageEventMessage);
            taskEntity.setTopic(sendRebateMessageEvent.topic());
            taskEntity.setState(taskStateVo.create);

            behaviorRebateAggregates.add(BehaviorRebateAggregate.builder()
                    .userId(behaviorEntity.getUserId())
                    .behaviorRebateOrderEntity(behaviorRebateOrderEntity)
                    .taskEntity(taskEntity)
                    .build());
        }

        //3.存储聚合对象
        repository.saveUserRebateRecord(behaviorEntity.getUserId(),behaviorRebateAggregates);


        //4.返回订单id集合

        return orderIds;
    }

    @Override
    public List<BehaviorRebateOrderEntity> queryOrderByOutBusinessNo(String userId, String outBusinessNo) {
        return repository.queryOrderByOutBusinessNo(userId,outBusinessNo);
    }

}
